import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.Serializable;

import javax.swing.ButtonGroup;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;


//FIXME Remove rations setting of "None"? (Only happens when food == 0?)
//FIXME Remove pace setting of "Stopped"? (Add "Rest" button to Turn screen)

/**
 * Represents the configuration screen when a user selects New Game.
 * Created with Window Builder.
 */
public class ConfigurationPanel extends JPanel implements Serializable{
	
	//INSTANCE VARIABLES
	
	private Game game;
	
	private JTextField pnameField;
	private JTextField partyMem1Field;
	private JTextField partyMem2Field;
	private JTextField partyMem3Field;
	private JTextField partyMem4Field;
	private JRadioButton rdbtnBanker;
	private JRadioButton rdbtnCarpenter;
	private JRadioButton rdbtnFarmer;
	private JComboBox paceComboBox;
	private JComboBox rationsComboBox;
	private ButtonGroup professionGroup;
	private JButton btnStartGame;
	
	/**
	 * Creates the configuration screen.
	 * @param g - the current game.
	 */
	public ConfigurationPanel(Game g){
		
		game = g;
		setLayout(null);
		setPreferredSize(new Dimension(800,600));
		
		JLabel lblPlayerName = new JLabel("Player Name:");
		lblPlayerName.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblPlayerName.setBounds(29, 29, 86, 17);
		add(lblPlayerName);
		
		pnameField = new JTextField();
		pnameField.setBounds(125, 29, 170, 20);
		add(pnameField);
		pnameField.setColumns(30);
		
		rdbtnBanker = new JRadioButton("Banker");
		rdbtnBanker.setSelected(true);
		rdbtnBanker.setBounds(29, 186, 109, 23);
		add(rdbtnBanker);
		
		rdbtnCarpenter = new JRadioButton("Carpenter");
		rdbtnCarpenter.setBounds(169, 186, 109, 23);
		add(rdbtnCarpenter);
		
		rdbtnFarmer = new JRadioButton("Farmer");
		rdbtnFarmer.setBounds(322, 186, 109, 23);
		add(rdbtnFarmer);
		
		professionGroup = new ButtonGroup();
		professionGroup.add(rdbtnBanker);
		professionGroup.add(rdbtnCarpenter);
		professionGroup.add(rdbtnFarmer);
		
		JLabel lblChooseAProfession = new JLabel("Choose a Profession:");
		lblChooseAProfession.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblChooseAProfession.setBounds(29, 118, 155, 25);
		add(lblChooseAProfession);
		
		paceComboBox = new JComboBox();
		paceComboBox.setModel(new DefaultComboBoxModel(new String[] {"Stopped", "Leisurely", "Steady", "Grueling"}));
		paceComboBox.setFont(new Font("Tahoma", Font.PLAIN, 12));
		paceComboBox.setBounds(629, 28, 109, 20);
		add(paceComboBox);
		
		JLabel paceLabel = new JLabel("Initial Pace:");
		paceLabel.setFont(new Font("Tahoma", Font.PLAIN, 14));
		paceLabel.setBounds(551, 30, 68, 14);
		add(paceLabel);
		
		JLabel rationsLabel = new JLabel("Initial Rations:");
		rationsLabel.setFont(new Font("Tahoma", Font.PLAIN, 14));
		rationsLabel.setBounds(533, 122, 86, 17);
		add(rationsLabel);
		
		rationsComboBox = new JComboBox();
		rationsComboBox.setFont(new Font("Tahoma", Font.PLAIN, 12));
		rationsComboBox.setModel(new DefaultComboBoxModel(new String[] {"None", "Bare-Bones", "Meager", "Normal", "Well-Fed"}));
		rationsComboBox.setBounds(629, 121, 109, 20);
		add(rationsComboBox);
		
		JLabel lblPartyMemebers = new JLabel("Party Memebers:");
		lblPartyMemebers.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblPartyMemebers.setBounds(10, 381, 130, 20);
		add(lblPartyMemebers);
		
		JLabel lblPartyMember = new JLabel("Party Member 1:");
		lblPartyMember.setFont(new Font("Tahoma", Font.PLAIN, 12));
		lblPartyMember.setBounds(10, 424, 97, 14);
		add(lblPartyMember);
		
		JLabel lblPartyMember_1 = new JLabel("Party Member 2:");
		lblPartyMember_1.setFont(new Font("Tahoma", Font.PLAIN, 12));
		lblPartyMember_1.setBounds(10, 449, 97, 14);
		add(lblPartyMember_1);
		
		JLabel lblPartyMember_2 = new JLabel("Party Member 3:");
		lblPartyMember_2.setFont(new Font("Tahoma", Font.PLAIN, 12));
		lblPartyMember_2.setBounds(10, 474, 97, 14);
		add(lblPartyMember_2);
		
		JLabel lblPartyMember_3 = new JLabel("Party Member 4:");
		lblPartyMember_3.setFont(new Font("Tahoma", Font.PLAIN, 12));
		lblPartyMember_3.setBounds(10, 499, 97, 17);
		add(lblPartyMember_3);
		
		partyMem1Field = new JTextField();
		partyMem1Field.setColumns(30);
		partyMem1Field.setBounds(125, 422, 170, 20);
		add(partyMem1Field);
		
		partyMem2Field = new JTextField();
		partyMem2Field.setColumns(30);
		partyMem2Field.setBounds(125, 447, 170, 20);
		add(partyMem2Field);
		
		partyMem3Field = new JTextField();
		partyMem3Field.setColumns(30);
		partyMem3Field.setBounds(125, 472, 170, 20);
		add(partyMem3Field);
		
		partyMem4Field = new JTextField();
		partyMem4Field.setColumns(30);
		partyMem4Field.setBounds(125, 498, 170, 20);
		add(partyMem4Field);
		
		btnStartGame = new JButton("Start Game");
		btnStartGame.setBounds(612, 497, 126, 23);
		add(btnStartGame);
		btnStartGame.addActionListener(new ButtonHandler());
	}
	
	/**
	 * Retrieves the strings held in the text boxes for the party leader and party member names.
	 * @return String array holding the party leader's name in index 0 and the four party members' names in subsequent indices, in natural order.
	 */
	public String[] getNames()
	{
		String[] names = new String[5];
		
		names[0] = pnameField.getText();
		names[1] = partyMem1Field.getText();
		names[2] = partyMem2Field.getText();
		names[3] = partyMem3Field.getText();
		names[4] = partyMem4Field.getText();
		
		return names;
	}
	
	/**
	 * Constructs a profession based on the selection of the user
	 * @return A Profession that is a Banker, Carpenter or Farmer, whichever the user chose.
	 */
	public Profession getProfession()
	{
		if(rdbtnBanker.isSelected())
			return new Banker();
		else if(rdbtnCarpenter.isSelected())
			return new Carpenter();
		else
			return new Farmer();
	}
	
	/**
	 * Gets the pace selection of the user
	 * @return Pace selection as an index of the comboBox.
	 */
	public int getPaceIndex()
	{
		return (paceComboBox.getSelectedIndex());
	}
	
	/**
	 * Gets the ration selection of the user
	 * @return Ration selection as an index of the comboBox.
	 */
	public int getRationsIndex()
	{
		return rationsComboBox.getSelectedIndex();
	}//ends getRationsIndex

	
	private class ButtonHandler implements ActionListener {
		
		public void actionPerformed(ActionEvent buttonClick) 
		{
			if (buttonClick.getSource().equals(btnStartGame)){				
				if (!(pnameField.getText().equals("") 
						|| partyMem1Field.getText().equals("") 
						|| partyMem2Field.getText().equals("") 
						|| partyMem3Field.getText().equals("") 
						|| partyMem4Field.getText().equals("") 
						|| (!rdbtnBanker.isSelected() 
								&& !rdbtnCarpenter.isSelected() 
								&& !rdbtnFarmer.isSelected()) ))
				{
					// Configuration info entered correctly
					game.configuration();
					game.changePanel("StorePanel");
				}
					
					
				else {
					//Configuration info not entered correctly
					//TODO Make this a pop-up dialog box or something
					System.out.println("Not done, sir.");
				}
				
			}//ends if
		}//ends actionPerformed
	}//ends ButtonHandler
}//ends ConfigurationPanel